package com.lagou.homework02;
import java.util.Scanner;
public class Gobang {
    //声明一个16*16的二维数组；
    public String[][] board = new String[16][16];
    //声明一个布尔类型的成员变量，方便之后再下棋的成员方法chess中落子的黑白方以及输入值bw的转换
    boolean flag = true;
    //自定义成员方法完成棋盘的绘制；
    public void draw()  {
        for(int i = 0; i < board.length; i++)   {
            for(int j = 0; j < board.length; j ++ ) {
                board[i][j] = "+";
            }
        }
    }
    // 直接使用模块一作业中棋盘绘制的代码，稍作调整
    public void show()   {
        for (int i = 0; i < 17; i++) {
            if (0 == i) {
                System.out.print("  ");
            } else {
                // 按照十六进制的格式打印i-1的数值
                System.out.printf("%x ", i - 1);
            }
        }
        System.out.println();
        // 2.2 绘制棋盘中除了第一行之外的其它部分以及行坐标信息
        for (int i = 0; i < 16; i++) {
            // 用于打印行坐标信息
            System.out.printf("%x ", i);
            for (int j = 0; j < 16; j++) {
                // 刚开始棋盘中的所有内容都是+，因此直接打印，但是为了完成后续功能这里需要借助二维数组（先留个悬念哦！）
                System.out.print(board[i][j] + " ");
            }
            // 打印完毕一行的所有内容之后进行换行
            System.out.println();
        }
    }
            
    public void chess() {
        System.out.println("请" +  (flag? "执黑": "执白") + "方输入要落子的行坐标，列坐标,范围为(0,0) 到 (15,15): ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            while( x < 0 | x > 15 | y < 0 | y > 15) {
                System.out.println("您输入坐标数值超出范围，请重新输入：");
                x = sc.nextInt();
                y = sc.nextInt();
            }

            while (board[x][y] != "+")  {
                System.out.println("该位置已有棋子，请重新输入：");
                x = sc.nextInt();
                y = sc.nextInt();
            }
            board[x][y] = flag?"b":"w";
            flag = !flag;
        System.out.println("目前的棋盘落子情况是：");
        this.show();
    }

    public boolean inBoard(int[] point) {// 判断point是否在棋盘上
        int x = point[0];
        int y = point[1];
        if (x < 0 || y < 0 || x >= board.length || y >= board.length)
            return false;
        else
            return true;

    }
    //自定义成员方法实现对获胜的判断1表示白棋胜,-1表示黑棋胜,0表示还在下棋中
    public int win() {
        int whoWin = 0;

        int[] point = new int[2];
        outer: while (true) {
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board.length; j++) {

                    if (board[i][j] == "w") {// 遍历数组，找到的第一个白棋
                        point[0] = i;
                        point[1] = j + 4;

                        if (inBoard(point)) {// 如果存在向右数4个的棋盘
                            if (board[i][j + 1] == "w" && board[i][j + 2] == "w" && board[i][j + 3] == "w" && board[i][j + 4] == "w") {
                                whoWin = 1;
                                break outer;
                            }
                        }

                        point[0] = i + 4;
                        point[1] = j + 4;

                        if (inBoard(point)) {// 如果存在向右,向下数4个的棋盘
                            if (board[i + 1][j + 1] == "w" && board[i + 2][j + 2] == "w" && board[i + 3][j + 3] == "w"
                                    && board[i + 4][j + 4] == "w") {
                                whoWin = 1;
                                break outer;
                            }
                        }

                        point[0] = i + 4;
                        point[1] = j;

                        if (inBoard(point)) {// 如果存在向下数4个的棋盘
                            if (board[i + 1][j] == "w" && board[i + 2][j] == "w" && board[i + 3][j] == "w" && board[i + 4][j] == "w") {
                                whoWin = 1;
                                break outer;
                            }
                        }

                        point[0] = i - 4;
                        point[1] = j + 4;

                        if (inBoard(point)) {// 如果存在向左,向下数4个的棋盘
                            if (board[i - 1][j + 1] == "w" && board[i - 2][j + 2] == "w" && board[i - 3][j + 3] == "w"
                                    && board[i - 4][j + 4] == "w") {
                                whoWin = 1;
                                break outer;
                            }
                        }

                    } else if (board[i][j] == "b") {
                        point[0] = i;
                        point[1] = j + 4;

                        if (inBoard(point)) {// 如果存在向右数4个的棋盘
                            if (board[i][j + 1] == "b" && board[i][j + 2] == "b" && board[i][j + 3] == "b" && board[i][j + 4] == "b") {
                                whoWin = -1;
                                break outer;
                            }
                        }

                        point[0] = i + 4;
                        point[1] = j + 4;

                        if (inBoard(point)) {// 如果存在向右,向下数4个的棋盘
                            if (board[i + 1][j + 1] == "b" && board[i + 2][j + 2] == "b" && board[i + 3][j + 3] == "b"
                                    && board[i + 4][j + 4] == "b") {
                                whoWin = -1;
                                break outer;
                            }
                        }

                        point[0] = i + 4;
                        point[1] = j;

                        if (inBoard(point)) {// 如果存在向下数4个的棋盘
                            if (board[i + 1][j] == "b" && board[i + 2][j] == "b" && board[i + 3][j] == "b" && board[i + 4][j] == "b") {
                                whoWin = -1;
                                break outer;
                            }
                        }

                        point[0] = i - 4;
                        point[1] = j + 4;

                        if (inBoard(point)) {// 如果存在向左,向下数4个的棋盘
                            if (board[i - 1][j + 1] == "b" && board[i - 2][j + 2] == "b" && board[i - 3][j + 3] == "b"
                                    && board[i - 4][j + 4] == "b") {
                                whoWin = -1;
                                break outer;
                            }
                        }

                    }

                }
            }
            whoWin = 0;
            break outer;

        }
        return whoWin;
    }


}

//先判断是否在棋盘上的部分是网上摘抄的，判断胜负的思路是自己想的，一开始没有考虑到判断j+4的棋子要在棋盘上；